package shj.blackberry.i.util;

import java.util.Random;

/**
 * 卦对象
 * 
 * @author shan.hongjie
 * 
 */
public class GuaXiang {
	public int getIntBenGua() {
		return intBenGua;
	}

	public void setIntBenGua(int intBenGua) {
		this.intBenGua = intBenGua;
	}

	public String getHuGua() {
		return huGua;
	}

	public void setHuGua(String huGua) {
		this.huGua = huGua;
	}

	public String getZhiGua() {
		return zhiGua;
	}

	public void setZhiGua(String zhiGua) {
		this.zhiGua = zhiGua;
	}

	public String getCuoGua() {
		return cuoGua;
	}

	public void setCuoGua(String cuoGua) {
		this.cuoGua = cuoGua;
	}

	public String getZongGua() {
		return zongGua;
	}

	public void setZongGua(String zongGua) {
		this.zongGua = zongGua;
	}

	public static GuaXiang getGuaXiang() {
		return guaXiang;
	}

	public static void setGuaXiang(GuaXiang guaXiang) {
		GuaXiang.guaXiang = guaXiang;
	}

	public void setBenGua(String benGua) {
		this.benGua = benGua;
	}

	public void setYaoFlag(String yaoFlag) {
		this.yaoFlag = yaoFlag;
	}

	int intBenGua;
	/**
	 * 本卦(长度为6的0/1字符串)
	 */
	String benGua = "111111";
	/**
	 * 变爻标识：1代表爻变，第一位代表第一爻
	 */
	String yaoFlag = "000000";
	public String getYaoFlag() {
		return yaoFlag;
	}

	/**
	 * 互卦
	 */
	String huGua = "";
	/**
	 * 之卦
	 */
	String zhiGua = "";
	/**
	 * 错卦
	 */
	String cuoGua = "";
	/**
	 * 综卦
	 */
	String zongGua = "";

	/**
	 *一个卦象类只有一实例卦
	 */
	private static GuaXiang guaXiang;

	/**
	 * construct function,only public one can be used for constructing object
	 * outside of this class,private one can only be used in just this class.
	 * 
	 */
	private GuaXiang(char[] charsGuaXiang) throws Exception {
		StringBuffer sbBenGua = new StringBuffer();
		StringBuffer sbZhiGua = new StringBuffer();
		StringBuffer sbYaoFlag = new StringBuffer();
		StringBuffer sbCuoGua = new StringBuffer();
		for (int i = 1; i < charsGuaXiang.length; i = i + 2) {
			if (charsGuaXiang[i - 1] == '1') {
				// 阳
				sbBenGua.append("1");
				sbCuoGua.append("0");
				if (charsGuaXiang[i] == '1') {
					sbYaoFlag.append("1");
					sbZhiGua.append("0");
				} else {
					sbYaoFlag.append("0");
					sbZhiGua.append("1");
				}
			} else {
				// 阴
				sbCuoGua.append("1");
				sbBenGua.append("0");
				if (charsGuaXiang[i] == '0') {
					sbYaoFlag.append("1");
					sbZhiGua.append("1");
				} else {
					sbYaoFlag.append("0");
					sbZhiGua.append("0");
				}
			}
		}
		benGua = sbBenGua.toString();
		yaoFlag = sbYaoFlag.toString();
		zhiGua = sbZhiGua.toString();
		cuoGua = sbCuoGua.toString();
		StringBuffer sbZongGua = new StringBuffer();
		char[] charsBenGua = benGua.toCharArray();
		for(int i=charsBenGua.length-1;i>=0;i--){
			sbZongGua.append(charsBenGua[i]);
		}
		zongGua = sbZongGua.toString();
		huGua = ""+ charsBenGua[1]+charsBenGua[2]+charsBenGua[3]+charsBenGua[2]+charsBenGua[3]+charsBenGua[4];
	}

	/**
	 * 本类只产生一卦
	 * 
	 * @return
	 */
	public static GuaXiang GuaXiang(String inputStr) throws Exception {
		if (inputStr == null || inputStr.length() != 12) {
			throw new Exception("卦象输入有误");
		} else {
			char[] charsGuaXiang = inputStr.toCharArray();
			for (int i = 0; i < charsGuaXiang.length; i++) {
				if (charsGuaXiang[i] != '1' && charsGuaXiang[i] != '0') {
					throw new Exception("卦象输入有误");
				}
			}
			guaXiang = new GuaXiang(charsGuaXiang);
			return guaXiang;
		}

	}
	
	
	/**
	 * 生成随机的卦象字符串(01十二位序列)
	 * @return
	 */
	public static String getRandomStringImage() {
		StringBuffer sbRandomImage = new StringBuffer();
		Random r = new Random();
		int tmp=0;
		for(int i=0;i<6;i++){
			//按3铜币方式生成
			int yangCount = 0;//3次随机阳数统计
			for(int j=0;j<3;j++){
				tmp = r.nextInt(2);
				if(tmp==1){
					yangCount++;
				}
			}
			if(yangCount==0){
				sbRandomImage.append("00");	
			}else if(yangCount==1){
				sbRandomImage.append("01");	
			}else if(yangCount==2){
				sbRandomImage.append("10");	
			}else if(yangCount==3){
				sbRandomImage.append("11");	
			}
		}
		return sbRandomImage.toString();
	}

	
	/**
	 * 获取本卦卦象
	 * @return
	 */
	public String getBenGua(){
		return benGua;
	}

	/**
	 * 获取本卦文本
	 * @return
	 */
	public String getBenGuaCi(){
		
		return FileAccess.getStringFromDataDirFile(benGua);
	}

	/**
	 * 获取互卦文本
	 * @return
	 */
	public String getHuGuaCi(){
		return FileAccess.getStringFromDataDirFile(huGua);
	}

	/**
	 * 获取之卦文本
	 * @return
	 */
	public String getZhiGuaCi(){
		return FileAccess.getStringFromDataDirFile(zhiGua);
	}

	/**
	 * 获取错卦文本
	 * @return
	 */
	public String getCuoGuaCi(){
		return FileAccess.getStringFromDataDirFile(cuoGua);
	}

	/**
	 * 获取综卦文本
	 * @return
	 */
	public String getZongGuaCi(){
		return FileAccess.getStringFromDataDirFile(zongGua);
	}
}
